/**
 * 
 */
package com.pointmi.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.forte.shoppingcustomer.ShoppingCustomerService;
import com.forte.shoppingcustomerdao.ShoppingCustomer;
import com.pointmi.model.LoginModel;

/**
 * Controller for user log in 
 * @author AMINA
 */
@Controller
@SessionAttributes("customer")
public class LoginController {
	@Autowired
	private ShoppingCustomerService customerService;

	@RequestMapping(value="/login.do", method= RequestMethod.POST)
	public String login(@Valid LoginModel login, BindingResult result, Model model)
	{
		String returnPage = "login";
		ShoppingCustomer customer;
		if(login != null) {
			customer = customerService.findCustomerByEmail(login.getemail());
			if(customer != null) {
				customer.setPassword(null);
				model.addAttribute("customer", customer);
				returnPage = "product";
			}
		} //else
		return returnPage;
	}
	
	 @RequestMapping(value="/login.do")
		public String login(Model model)
		{
			model.addAttribute("login", new LoginModel());
			return "login";
		}

}
